In programming the order of execution of instructions may have to be changed depending on certain conditions. This involves a kind of decision making to see weather a particular condition has occurred or not and then direct the computer to execute certain instructions accordingly.
Decision making with if statement:
-
The if statement is a powerful decision-making statement and is used to control the flow of execution of statements.
-
It is basically a two-way decision statement and is used in conjunction with an expression.
-
It allows the computer to evaluate the expression first and then, depending on weather the value of the expression (relation or condition) is ‘true’ (or non-zero) or ‘false’ (zero), it transfers the control to a particular statement.
-
The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are:
- ) Simple if statement
- ) if……else statement
- ) Nested if…….else statement
- ) else if ladder
Simple If Statement
where: | |
statement-block - | may be a single statement or a group of
statements. |
test_expression - | it is a conditional expression |
-
If the test_expression is true, the statement-block executed and then statement-x executed; otherwise the statement-block will be skipped and the execution will jump to the statement-x.
If…..else Statement
- if….else statement is an extension of the simple if statement.
- If the test expression is true, then the true-block statement(s), immediately following the if statements are executed;
otherwise, the false-block statement(s) are executed. In either case, either true-block or false-block will be executed, not both.
If…..else if Ladder
- The if….else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
-
The if….else ladder allows you to check between multiple test expressions and execute different statements.
Nesting of if…..else statement
- Incorporating multiple if-else statements within an if and else statement is known as nesting. Nested if-else in C involves using an if-else statement inside the if statement block.
Conditional Operator
- Useful for making two-way decisions.
- Also known as Ternary Operator.
- This operator is a combination of ‘?’ and ‘:’ and takes three operands.
- The conditional_expression is evaluated first.
- If the result is nonzero, expression1 is evaluated and is returned as the value of the conditional expression.
- Otherwise, expression2 is evaluated and its value is returned.
goto Statement
- goto statement is known as jump statement in C.
- goto is used to transfer the program control to a predefined label.
- goto statement can be used to repeat some part of code for a particular condition.
- goto statement can also be used to break the multiple loops which can not be done by using a single break statement.
switch Statement
- The control statement that allows us to make a decision form the number of choices is called a switch statement.
- The switch statement tests the value of a given variable (or expression) against a list off case value and when a match is found, a block of statement associated with the case is executed.
- break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement.
- default is an optional case. When present, it will be executed if the value of the expression does not match with any of the case value